package com.example.treasure_back.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.example.treasure_back.config.AliOSSConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.InputStream;

@Component
public class AliOSSUtils {

    @Autowired
    private AliOSSConfig aliOSSConfig;

    public boolean uploadAvatar(InputStream inputStream, String filepath){
        // 创建示例
        OSS ossClient = new OSSClientBuilder().build(aliOSSConfig.ENDPOINT, aliOSSConfig.ACCESS_KEY_ID, aliOSSConfig.ACCESS_KEY_SECRET);
        // 上传图片
        ossClient.putObject(aliOSSConfig.BUCKET, filepath,inputStream);
        // 关闭oss连接
        ossClient.shutdown();
        return true;
    }
}
